#!/usr/bin/env bash

# Hold at 9.1.x until we can figure out this issue currently present in 9.2.2:
# https://github.com/apache/trafficserver/issues/10393 This issue has been
# reproducible by running the following test in a loop:
# bundle exec minitest test/apis/test_web_app_large_body.rb -n test_limit_is_configurable
trafficserver_version="9.1.4"
trafficserver_hash="f18aca66e444470738f6031366edc45db26942917e3fe8cba9fbb3b28173e01e06991aa9846e459047365946738931b1910719574c9c2afee72e4dfb30eab617"

set -e -u -x
source ./tasks/helpers.sh
source ./tasks/helpers/detect_os_release.sh

task_working_dir
download "https://archive.apache.org/dist/trafficserver/trafficserver-$trafficserver_version.tar.bz2" "sha512" "$trafficserver_hash"
extract_download "trafficserver-$trafficserver_version.tar.bz2"

detect_os_release
if [[ "$ID_NORMALIZED" == "rhel" && "$VERSION_ID" == "7" ]]; then
  export CC=/opt/rh/devtoolset-7/root/usr/bin/gcc
  export CXX=/opt/rh/devtoolset-7/root/usr/bin/g++
elif [[ "$ID" == "debian" && "$VERSION_ID" == "9" ]]; then
  export CC=clang-7
  export CXX=clang++-7
  export CXXFLAGS="-I/usr/lib/llvm-7/include/c++/v1 -stdlib=libc++"
fi

cd "trafficserver-$trafficserver_version"

SPHINXBUILD=false ./configure \
  --disable-tests \
  --disable-silent-rules \
  --disable-maintainer-mode \
  --disable-dependency-tracking \
  --prefix="$INSTALL_PREFIX_EMBEDDED" \
  --with-jemalloc \
  --with-luajit="$STAGE_EMBEDDED_DIR/openresty/luajit"
make -j"$NPROC"
make install DESTDIR="$STAGE_DIR"
chrpath -d "$STAGE_EMBEDDED_DIR/lib/libtscore.so"
chrpath -d "$STAGE_EMBEDDED_DIR/lib/libtsmgmt.so"
find "$STAGE_EMBEDDED_DIR/libexec/trafficserver/" -name "*.so" -exec chrpath -d {} \;
find "$STAGE_EMBEDDED_DIR/bin/" -name "traffic_*" -exec chrpath -d {} \;

stamp
